package com.zxx.coolweather._wuguan2.queue_prority;

import java.util.concurrent.BlockingQueue;

/**
 * @author zxx on 2022/1/12
 */
public class TaskExecutorMy extends Thread {

    private BlockingQueue<Request> blockingQueue;
    private boolean isRunning = true;

    public TaskExecutorMy(BlockingQueue<Request> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    public void setRunning(boolean running) {
        isRunning = running;
    }

    @Override
    public void run() {
        while (isRunning) {
            Request request;
            try {
                // take方法是一个阻塞的方法，每次调用会拿到队列中的第一个任务，如果队列为空，这个方法将一直阻塞，知道队列中有任务再次返回
                request = blockingQueue.take();
            } catch (Exception e) {
                return;
            }

            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(request.getName());
        }
    }
}
